<?php
declare (strict_types=1);

namespace app\api\controller;

use app\Request;
use think\cache\driver\Redis;
use think\facade\Db;
use Workerman\Worker;

class Index
{
    public function index(Request $request)
    {
        $param = $request->param();
        $device_id = $param["device_id"];
        if (!$device_id) {
            return app("json")->fail("请选择设备");
        }
        $deviceData = Db::name("store_device")->where("id", $device_id)->where("is_del", 0)->find();
        $mask = strToHex($deviceData["bao_tou"] ?: "MyBr");
        $ver_mini = strToHex($deviceData["ver_mini"] ?: 0);
        $ver_high = strToHex($deviceData["ver_high"] ?: 0);
        $station_id = strToHex($deviceData["station_id"]);
        $module_id = strToHex($deviceData["module_id"]);
        $pwd = strToHex($deviceData["pwd"]);
        $tServer = strToHex(time());
        $cmd = strToHex("1");   // 开始洗车
        // 生成洗车的相关的秘钥
        $sendMsg = $mask . $ver_mini . $ver_high . $station_id . strToHex($device_id) . $module_id . $pwd . $tServer . $cmd;
        $redis = new Redis();
        $wash_machine_data = $redis->get("wash_machine_data");
        if (!$wash_machine_data) {
            $wash_machine_data[] = $sendMsg;
            $redis->set("wash_machine_data", $wash_machine_data);
        } else {
            if (in_array($sendMsg, $wash_machine_data)) {
                return app("json")->fail("请勿重复点击");
            } else {
                $wash_machine_data[] = $sendMsg;
                $redis->set("wash_machine_data", $wash_machine_data);
            }
        }
        return app("json")->successful("正在准备洗车");
    }

    /**
     * 客户端
     */
    public function subscribe()
    {
        $worker = new Worker();
        $worker->onWorkerStart = function () {
            $mqtt = new \Workerman\Mqtt\Client("mqtt://127.0.0.1:1883");
            $mqtt->onConnect = function ($mqtt) {
                $mqtt->subscribe('test');
            };
            $mqtt->onMessage = function ($topic, $content) {
                var_dump($topic, $content);
            };
            $mqtt->connect();
        };
        $worker::runAll();
    }

    /**
     * 服务端
     */
    public function publish()
    {
        $worker = new Worker();
        $worker->onWorkerStart = function () {
            $mqtt = new \Workerman\Mqtt\Client('mqtt://127.0.0.1:1883');
            $mqtt->onConnect = function ($mqtt) {
                $mqtt->subscribe('test');
            };
            $mqtt->onMessage = function ($topic, $content) {
                var_dump($topic, $content);
            };
            $mqtt->connect();
        };
        Worker::runAll();
    }

    /**
     * 定时访问的接口|优惠券
     * @param Request $request
     */
    public function timeStruct(Request $request)
    {
        Db::name("store_coupon_user")
            ->where("status", "in", ["1", "3"])
            ->select()->each(function ($item, $key) {
                echo $item["end_time"].PHP_EOL;
                if ($item["end_time"] <= time()) {
                    Db::name("store_coupon_user")->where("id", $item["id"])->update(["status" => 2]);
                }
        });
    }
}
